/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.domain.payment;

import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 * Provides a list of payment methods.  As with all Java collections, this
 * list is not thread safe by default.
 * 
 * @author Scott Rossillo
 *
 */
public class PaymentMethodList extends AbstractList implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private final List paymentMethods = new ArrayList();
	
	/**
	 * Creates a new payment method list.
	 */
	public PaymentMethodList() { }
	
	/**
	 * Removes all payment methods from this payment method list.
	 */
	public void clear() {
		paymentMethods.clear();
	}

	/**
	 * Adds the given payment method to this payment
	 * method list.
	 * 
	 * @param paymentMethod the <code>PaymentMethod</code> to
	 * add to this list; may not be <code>null</code>
	 * 
	 * @throws IllegalArgumentException if the given <code>paymentMethod</code>
	 * is <code>null</code>
	 */
	public final void add(PaymentMethod paymentMethod) {
		
		if(paymentMethod == null) {
			throw new IllegalArgumentException("[PaymentMethod] cannot be null!");
		}
		
		paymentMethods.add(paymentMethod);
	}

	/* (non-Javadoc)
	 * @see java.util.AbstractList#get(int)
	 */
	public Object get(int index) {
		return paymentMethods.get(index);
	}
	
	/**
	 * Returns the first account in this payment method list.
	 * 
	 * @return the first <code>Account</code> in this list 
	 * if at least one account payment method exists;
	 * <code>null</code> otherwise
	 */
	public Account getAccount() {

		List accounts = this.getAccounts();
		
		return (Account) (!accounts.isEmpty() ? accounts.get(0) : null);
	}
	
	/**
	 * Returns a list of all account payment methods in this list.
	 * 
	 * @return a <code>List</code> containing all <code>Account</code>
	 * payment methods if any exist; an emty <code>List</code> otherwise
	 */
	public List getAccounts() {
		
		List accounts = new ArrayList();
		PaymentMethod paymentMethod;
		
		for(Iterator it = paymentMethods.iterator(); it.hasNext(); ) {
			
			paymentMethod = (PaymentMethod) it.next();
			if(paymentMethod instanceof Account) {
				accounts.add(paymentMethod);
			}
		}
		
		return Collections.unmodifiableList(accounts);
	}
	
	/**
	 * Returns a single Bill Me Later account from this payment
	 * method list.
	 * 
	 * @return the first <code>BillMeLaterAccount</code> in this list
	 * if found; <code>null</code> otherwise
	 */
	public BillMeLaterAccount getBillMeLaterAccount() {
		
		List billMeLaterAccounts = this.getBillMeLaterAccounts();
		
		return (BillMeLaterAccount) (!billMeLaterAccounts.isEmpty() ? billMeLaterAccounts.get(0) : null);
	}
	
	/**
	 * Returns the Bill Me Later payment methods in this list.
	 * 
	 * @return a <code>List</code> of <code>BillMeLaterAccount</code> payment
	 * methods or an empty list of no Bill Me Later accounts are found
	 */
	public List getBillMeLaterAccounts() {
		
		List billMeLaterAccounts = new ArrayList();
		PaymentMethod paymentMethod;
		
		for(Iterator it = paymentMethods.iterator(); it.hasNext(); ) {
			
			paymentMethod = (PaymentMethod) it.next();
			if(paymentMethod instanceof BillMeLaterAccount) {
				billMeLaterAccounts.add(paymentMethod);
			}
		}
		
		return Collections.unmodifiableList(billMeLaterAccounts);
	}
	
	/**
	 * Returns the first credit card payment method
	 * from this list.
	 * 
	 * @return the first <code>CreditCard</code> payment in this
	 * payment method list if one exists; <code>null</code> otherwise
	 */
	public CreditCard getCreditCard() {
		
		List creditCards = this.getCreditCards();
		
		return (CreditCard) (!creditCards.isEmpty() ? creditCards.get(0) : null);
	}
	
	/**
	 * Returns the credit card payment methods in this list.
	 * 
	 * @return a <code>List</code> of <code>CreditCard</code> payment
	 * methods or an empty list of no credit card payments are found
	 */
	public List getCreditCards() {
		
		List creditCards = new ArrayList();
		PaymentMethod paymentMethod;
		
		for(Iterator it = paymentMethods.iterator(); it.hasNext(); ) {
			
			paymentMethod = (PaymentMethod) it.next();
			if( (paymentMethod instanceof CreditCard) && !(paymentMethod instanceof BillMeLaterAccount) ) {
				creditCards.add(paymentMethod);
			}
		}
		
		return Collections.unmodifiableList(creditCards);
	}

	/* (non-Javadoc)
	 * @see java.util.AbstractList#iterator()
	 */
	public Iterator iterator() {
		// FIXME: do we really want to open this up to the outside?
		return paymentMethods.iterator();
	}

	/**
	 * Removes the given payment method from this
	 * payment method list.
	 * 
	 * @param paymentMethod the <code>paymentMethod</code>
	 * to remove
	 * 
	 * @return <code>true</code> if this list contained
	 * the given <code>paymentMethod</code> and it was
	 * removed successfully; <code>false</code> otherwise
	 */
	public boolean remove(PaymentMethod paymentMethod) {
		return paymentMethods.remove(paymentMethod);
	}
	
	/**
	 * Removes all accounts from this list of payment methods.
	 */
	public void removeAllAccounts() {
		
		for(Iterator it = this.getAccounts().iterator(); it.hasNext(); ) {
			paymentMethods.remove( (PaymentMethod) it.next());
		}
	}
	
	/**
	 * Removes all Bill Me Later accounts from this list of payment methods.
	 */
	public void removeAllBillMeLaterAccounts() {
		
		for(Iterator it = this.getBillMeLaterAccounts().iterator(); it.hasNext(); ) {
			paymentMethods.remove( (PaymentMethod) it.next());
		}
	}
	
	/**
	 * Removes all credit cards from this list of payment methods.
	 */
	public void removeAllCreditCards() {
		
		for(Iterator it = this.getCreditCards().iterator(); it.hasNext(); ) {
			paymentMethods.remove( (PaymentMethod) it.next());
		}
	}
	
	/* (non-Javadoc)
	 * @see java.util.AbstractCollection#size()
	 */
	public int size() {
		return paymentMethods.size();
	}

}
